package com.freemalll.merchant.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.freemalll.merchant.constant.ConfigConstant;
import com.freemalll.merchant.domain.dto.DTOInnerUseParseConfig;
import com.freemalll.merchant.domain.dto.DTOInterfaceParams;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;


/**
 * @description: 接口平台调用
 * @author: wmh
 * @createDate: 2022-12-30
 * @version: 1.0
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class InterfaceOperational {


    /**
     * 获取【接口平台用户分析接口】的调用参数
     *
     * @author yangjun
     * @date 2022/12/19
     */
    public DTOInnerUseParseConfig getInnerUserParseConfig() {
        String innerUserParseConfig = ConfigUtil.getCoValue(ConfigConstant.INNER_USER_PARSE_CONFIG);
        if (StringUtils.isBlank(innerUserParseConfig)) {
            return null;
        }
        return JSON.parseObject(innerUserParseConfig, DTOInnerUseParseConfig.class);
    }

    public Result send(DTOInterfaceParams params) throws Exception {
        //获取接口配置
        DTOInnerUseParseConfig configInfo = getInnerUserParseConfig();
        if (configInfo == null) {
            return Result.error("接口平台调用账户为空");
        }
        StringBuilder urlSb = null;
        String sendType = params.getSendType();
        String upt = params.getUpt();
        String relationId = params.getRelationId();
        String interfaceName = params.getInterfaceName();
        String tableName = params.getTableName();
        String type = params.getType();
        if (DTOInterfaceParams.aliAddressStandard.equals(sendType)) {
            //阿里云银行卡四要素核验
            DTOInnerUseParseConfig.AliAddressStandard addressStandard = configInfo.getAliAddressStandard();
            urlSb = new StringBuilder(addressStandard.getUrl()).append("/").append(addressStandard.getAppid())
                    .append("/").append(addressStandard.getThirdAccCode())
                    .append("/").append(addressStandard.getApiCode())
                    .append("?businessId=").append(relationId);
            if (StringUtils.isBlank(interfaceName)) {
                interfaceName = "阿里云地址解析";
            }

        } else if (DTOInterfaceParams.COMMON_TYPE.equals(type)) {
            //启信宝
            DTOInnerUseParseConfig.CommonInterfaceBase interfaceBase = configInfo.getByKey(sendType);
            urlSb = new StringBuilder(interfaceBase.getUrl()).append("/").append(interfaceBase.getAppid())
                    .append("/").append(interfaceBase.getThirdAccCode())
                    .append("/").append(interfaceBase.getApiCode())
                    .append("?businessId=").append(relationId);
            if (StringUtils.isBlank(interfaceName)) {
                interfaceName = "启信宝-".concat(sendType);
            }
        }

        if (urlSb == null) {
            return Result.error("接口平台调用账户为空");
        }
        //请求头
        JSONObject header = new JSONObject();
        header.put("Authorization", configInfo.getAuthorization());
        if (StringUtils.isNotBlank(configInfo.getSecret())) {
            header.put("Secret", configInfo.getSecret());
        }
        String respData = HttpClientUtil.sendPost(urlSb.toString(), header, upt, interfaceName, tableName, relationId, 3);
        log.info("====接口平台-用户信息分析接口:{}", respData);
        if (!CommonUtil.isJson(respData)) {
            log.info("====接口平台-用户信息分析接口：返回结果非json串：{}；获取返回结果异常", respData);
            return Result.error("接口响应异常，非json响应");
        }
        return Result.success("成功", respData);
    }

}
